package thread;

import java.util.concurrent.*;

public class CreateThread {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName() + "->" + i);
                }
            }
        });
        FutureTask<String> task = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "Callable的call方法执行了！";
            }
        });
        Thread thread4 = new Thread(task);

        ExecutorService executorService = Executors.newFixedThreadPool(6);
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName() + "--->hello word!");
                }
            }
        });

        Thread thread1 = new Thread1();
        Thread thread2 = new Thread(new Thread2());

        thread1.setName("线程一");
        thread2.setName("线程二");
        thread3.setName("线程三");
        thread4.setName("线程四");
        //设置线程优先级 10最高 1最低
        thread1.setPriority(10);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        System.out.println(task.get());
    }
}

class Thread1 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 10 == 0) {
                System.out.println(Thread.currentThread().getName() + "----------->让位了");
                Thread.yield();
            }
            System.out.println(Thread.currentThread().getName() + "->" + i);
        }
    }
}

class Thread2 implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "->" + i);
        }
    }
}
